package com.lh.job.task;

import com.lh.common.entity.admin.OrderAfterSale;
import com.lh.common.entity.admin.OrderAfterSaleLog;
import com.lh.common.entity.state.RemarkEnum;
import com.lh.common.enums.AfterStatusEnum;
import com.lh.common.utils.MathUtil;
import com.lh.job.config.PayPalConfig;
import com.lh.job.dao.OrderAfterSaleLogMapper;
import com.lh.job.dao.OrderAfterSaleMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author gze
 * @ClassName: orderAfterSaleTask
 * @Description: (售后订单定时任务)
 * @date: 2020-05-29 16:01
 */
@Component("orderAfterSaleTask")
@Slf4j
public class OrderAfterSaleTask implements ITask{

    @Autowired
    private OrderAfterSaleMapper orderAfterSaleMapper;

    @Autowired
    private OrderAfterSaleLogMapper orderAfterSaleLogMapper;

    @Autowired
    private PayPalConfig payPalConfig;

    /*
     * 订单待退款订单退款
     * */
    @Override
    public void run(String param) {
        String day = "";
        String day1 = "";
        if (StringUtils.isNotBlank(param) && param.split(",").length == 2){
            String[] dayArr = param.split(",");
            day = dayArr[0];
            day1 = dayArr[1];
        }

        logisticsTimeout(day);

        logisticsNotReceived(day1);
    }


    /*
     * 2天没有写物流地址，自动退货失败，插入售后记录表
     * */
    public void logisticsTimeout(String day) {
        log.info("填写物流超时任务开始！！！");
        boolean bool = MathUtil.isNumber(day);
        if (!bool || day == null) day = "5";
        //查询所有退货中 status = 01 并且处理退货时间超过5天未填写  user_fill_time_first is null && now() - handle_time>2
        List<OrderAfterSale> orderAfterSaleList = orderAfterSaleMapper.getNoExpress(day);

        List<OrderAfterSale> upAfterList = new ArrayList<>();
        List<OrderAfterSaleLog> saleLogList = new ArrayList<>();
        if(orderAfterSaleList != null && !orderAfterSaleList.isEmpty()){
            for (OrderAfterSale afterSale:orderAfterSaleList) {
                //更新售后单状态及原因
                OrderAfterSale upAfter = new OrderAfterSale();
                upAfter.setId(afterSale.getId());
                upAfter.setUpdateTime(new Date());
                upAfter.setStatus(AfterStatusEnum.RETURNTIMEOUT);
                upAfter.setFinishTime(new Date());
                upAfterList.add(upAfter);

                //插入售后LOG表
                OrderAfterSaleLog log = new OrderAfterSaleLog();
                log.setAfterSaleId(afterSale.getAfterSaleId());
                log.setUserId(afterSale.getUserId());
                log.setOldStatus(afterSale.getStatus());
                log.setNewStatus(AfterStatusEnum.RETURNTIMEOUT);
                log.setRemark(RemarkEnum.SHZT08);
                log.setCreateTime(new Date());
                log.setUpdateTime(new Date());
                saleLogList.add(log);
            }
            upAfterList.stream().forEach(orderAfterSaleMapper::updateByKey);
            saleLogList.stream().forEach(orderAfterSaleLogMapper::insert);
        }

        log.info("填写物流超时任务结束！！！");

    }

    /*
     * 用户填写好物流后 21天商家未收到货直接修改退货失败，插入售后记录表
     * */
    public void logisticsNotReceived(String day) {
        log.info("商家收货超时任务开始！！！");
        boolean bool = MathUtil.isNumber(day);
        if (!bool || day == null) day = "21";

        //查询所有退货中 status = 01 并且  user_fill_time_first is not null && now() - user_fill_time_first>21
        List<OrderAfterSale> orderAfterSaleList = orderAfterSaleMapper.getNoReceived(day);
        List<OrderAfterSale> upAfterList = new ArrayList<>();
        List<OrderAfterSaleLog> saleLogList = new ArrayList<>();
        if(orderAfterSaleList != null && !orderAfterSaleList.isEmpty()){
            for (OrderAfterSale afterSale:orderAfterSaleList) {
                //更新售后单状态及原因
                OrderAfterSale upAfter = new OrderAfterSale();
                upAfter.setId(afterSale.getId());
                upAfter.setUpdateTime(new Date());
                upAfter.setStatus(AfterStatusEnum.RETURNTIMEOUT);
                upAfterList.add(upAfter);

                //插入售后LOG表
                OrderAfterSaleLog log = new OrderAfterSaleLog();
                log.setAfterSaleId(afterSale.getAfterSaleId());
                log.setUserId(afterSale.getUserId());
                log.setOldStatus(afterSale.getStatus());
                log.setNewStatus(AfterStatusEnum.RETURNTIMEOUT);
                log.setRemark(RemarkEnum.SHZT09);
                log.setCreateTime(new Date());
                log.setUpdateTime(new Date());
                saleLogList.add(log);
            }
            upAfterList.stream().forEach(orderAfterSaleMapper::updateByKey);
            saleLogList.stream().forEach(orderAfterSaleLogMapper::insert);
        }
        log.info("商家收货超时任务结束！！！");
    }
}
